//
//  HomeViewController.swift
//  WeiBo
//
//  Created by Tony on 2017/12/20.
//  Copyright © 2017年 Tony. All rights reserved.
//

import UIKit

class HomeViewController: BaseViewController {

    // MARK: lazy
    lazy var titleButton: TitleButton = TitleButton()
    
    lazy var popoverDelegate: PopoverDelegate = {
        
        var del: PopoverDelegate = PopoverDelegate()
        
        del.callBack = { [weak self] (isPop: Bool) -> Void in
            self?.titleButton.isSelected = isPop
        }
        return del
    }()
    
    lazy var statusViewModelArr : [StatusViewModel] = {
        return [StatusViewModel]()
    }()
    
    // MARK: 回调
    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitorView.addRotationAnim()
        
        if isLogin == false {
            return;
        }
        
        // Cell自适应
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 200
        
        // 设置导航控制器
        setNavItem()
        
        // 加载微博
        loadStatus()

    }
}

// MARK: -数据源
extension HomeViewController {
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HOMECELL") as! HomeCell
        let statusViewModel = statusViewModelArr[indexPath.row]
        cell.viewModel = statusViewModel
        return cell
        
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModelArr.count
    }
}

// MARK: -初始化UI
extension HomeViewController {
    func setNavItem() {
        
        navigationItem.leftBarButtonItem = UIBarButtonItem.init(imageName: "navigationbar_friendattention")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem.init(imageName: "navigationbar_pop")
        
        navigationItem.titleView = titleButton
        titleButton.setTitle("coder", for: .normal)
        
        // 带参数的事件监听
        titleButton.addTarget(self, action: #selector(titleButtonClick(titleBtn:)), for: .touchUpInside)
        
    }
}

// MARK: -事件监听
extension HomeViewController {
    
    func titleButtonClick(titleBtn: TitleButton) {
        titleButton.isSelected = !titleButton.isSelected
        
        // 创建弹出的控制器材
        let popoverVc = PopoverViewController()
        
        // 设置控制器modal样式，能能够保证，modal后其底部view不被移除
        popoverVc.modalPresentationStyle = .custom
        
        // 设置frame
        let width = 180
        let height = 250
        let y = 55
        let x = Int(UIScreen.main.bounds.size.width * 0.5) - Int(Double(width) * 0.5)
        popoverDelegate.presentedViewFrame = CGRect.init(x: x, y: y, width: width, height: height)
        
        // 设置代理
        popoverVc.transitioningDelegate = popoverDelegate
        
        // modal
        present(popoverVc, animated: true, completion: nil)
    }
}

// MARK: -请求微博数据
extension HomeViewController {
    func loadStatus() {
        HttpTool.shareInstance.loadStatuses { ( result: [[String : AnyObject]]?, error: Error?) -> (Void) in
            
            // 错误校验
            if error != nil {
                
                mySelfLog(error?.localizedDescription)
                return
            }
            guard let resultArr = result else {
                return
            }
            
            // 遍历
            for statusDict in resultArr {
                let status = Status.init(dict: statusDict)
                let statusViewModel = StatusViewModel.init(status: status)
                self.statusViewModelArr.append(statusViewModel)

            }
            // 刷新表格
            self.tableView.reloadData()
        }
    }
}
